<?php
namespace admin\action;

use herosphp\bean\Beans;
use herosphp\http\HttpRequest;
use herosphp\utils\AjaxResult;

/**
 * manager action
 * @package admin\action
 * @author yangjian<yangjian102621@gmail.com>
 */
class ManagerAction extends CommonAction {

    protected $serviceBean = 'admin.manager.service';
    protected $actionTitle = '管理员';

    //数据列表页面
    public function index( HttpRequest $request ) {
        $name = $request->getParameter('name', 'trim');
        $mobile = $request->getParameter('mobile', 'trim');

        $conditions = array();
        if ( $name ) $conditions['name'] = "%{$name}%";
        if ( $mobile ) $conditions['mobile'] = "%{$mobile}%";

        $this->setConditions($conditions);
        parent::index($request);
        $this->setView('system/manager_index');
        $this->assign('breadcrumb', array('系统管理', $this->actionTitle.'列表'));
    }

    //数据添加页面
    public function add(HttpRequest $request) {
        $this->loadRoles();
        $this->setView('system/manager_add');
        $this->assign('breadcrumb', array('系统管理', $this->actionTitle.'添加'));
    }

    //数据编辑页面
    public function edit(HttpRequest $request) {
        $this->loadRoles();
        parent::edit($request);
        $this->setView('system/manager_edit');
        $this->assign('breadcrumb', array('系统管理', $this->actionTitle.'编辑'));
    }

    //加载角色
    protected function loadRoles() {
        $roleService = Beans::get('admin.role.service');
        $roles = $roleService->find();
        $this->assign('roles', $roles);
    }

    //插入数据操作
    public function insert(HttpRequest $request) {

        $data = $request->getParameter('data');
        $service = Beans::get($this->getServiceBean());
        if ( $service->count(array('mobile' => $data['mobile'])) > 0 ) {
            AjaxResult::ajaxResult(AjaxResult::OP_FAILURE, "该手机号码已被其他管理员使用.");
        }
        if ( $service->count(array('username' => $data['username'])) > 0 ) {
            AjaxResult::ajaxResult(AjaxResult::OP_FAILURE, "该用户名已被其他管理员使用.");
        }
        if ( $data['role_id'] == 0 && !$this->loginUser->isSuperManager() ) {
            AjaxResult::ajaxResult(AjaxResult::OP_FAILURE, LANG('SUPER_MANAGER_PERMISSION'));
        }
        $data['password'] = md5(md5($data['password']));
        parent::__insert($data);
    }

    //更新数据操作
    public function update(HttpRequest $request) {
        //AjaxResult::ajaxSuccessResult();
        $data = $request->getParameter('data');
        if ( trim($data['password']) != '' ) {
            $data['password'] = md5(md5($data['password']));
        } else {
            unset($data['password']);
        }
        if ( $data['role_id'] == 0 && !$this->loginUser->isSuperManager() ) {
            AjaxResult::ajaxResult(AjaxResult::OP_FAILURE, LANG('SUPER_MANAGER_PERMISSION'));
        }
        $id = $request->getParameter('id', 'trim');
        parent::__update($data, $id);

    }

    //更新个人信息
    public function profile(HttpRequest $request) {

        $data = $request->getParameter('data');
        $id = $this->loginUser->getId();
        $service = Beans::get($this->getServiceBean());

        $request->addParameter('logmessage', "修改了个人信息");
        parent::__update($data, $id, function() use($service, $id) {
            $manager = $service->getItem($id);
            $service->setLoginManager($manager);
        });

    }

    //修改密码
    public function password(HttpRequest $request) {
        $password = $request->getParameter('password', 'trim');
        $id = $this->loginUser->getId();
        $service = Beans::get($this->getServiceBean());
        if ( $service->set('password', md5(md5($password)), $id) ) {
            $request->addParameter('logmessage', '修改了本人的登录密码');
            AjaxResult::ajaxResult(AjaxResult::OP_SUCCESS, '密码修改成功.');
        } else {
            AjaxResult::ajaxResult(AjaxResult::OP_FAILURE, '密码修改失败.');
        }
    }

    public function delete(HttpRequest $request) {
        $id = $request->getParameter('id', 'trim');
        if ( $id == $this->loginUser->getId() ) {
            AjaxResult::ajaxResult(AjaxResult::OP_FAILURE, '不能删除自己的账户.');
        }
        parent::delete($request);
    }

}
